package com.ferryc.modules.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ferryc.commons.utils.StringUtil;
import com.ferryc.commons.web.BaseController;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.constant.ResultConstant;
import com.ferryc.modules.sys.entity.SysPermissions;
import com.ferryc.modules.sys.entity.param.SysPermissionsParam;
import com.ferryc.modules.sys.service.ISysPermissionsService;
import com.ferryc.modules.sys.service.ISysRoleService;

/**
 * 
 * 类名: SysPermissionsController <br/>
 * 作用： 控制<br/>
 * 方法： <br/>
 * 创建者: Ferry C <br/>
 * 添加时间: 2017年12月02日 04:20:09 <br/>
 */
@Controller
@RequestMapping("/permissions")
public class SysPermissionsController extends BaseController {

	/** */
	@Autowired
	private ISysPermissionsService sysPermissionsService;

	@Autowired
	private ISysRoleService sysRoleService;

	/** */
	@RequiresPermissions("permissions")
	@GetMapping("/")
	public String manage(Model model) {
		return "sys/permissions/list";
	}

	/**
	 * 列表查询
	 * 
	 * @param sysPermissionsParam
	 *            请求参数
	 * @return
	 */
	@RequiresPermissions("permissions")
	@PostMapping("/list")
	@ResponseBody
	public Object list(SysPermissionsParam sysPermissionsParam) {
		sysPermissionsParam.setStatus(StringUtil.EMPTY);
		return sysPermissionsService.selectPage(sysPermissionsParam.toSysPermissions());
	}

	/**
	 * 进入新增页面
	 * 
	 * @param sysLogInfoParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 *            request
	 * @return
	 */
	@RequiresPermissions("permissions_add")
	@GetMapping("/addPage")
	public Object addPage(SysPermissionsParam sysPermissionsParam, Model model, HttpServletRequest request) {
		model.addAttribute(GlobalConstant.PARENT_ID, request.getParameter(GlobalConstant.PARENT_ID));
		return "sys/permissions/add";
	}

	/**
	 * 新增保存
	 * 
	 * @param sysPermissionsParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("permissions_add")
	@PostMapping("/insert")
	@ResponseBody
	public Object insert(@Valid SysPermissionsParam sysPermissionsParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean flag = sysPermissionsService.checkPermissionName(sysPermissionsParam.toSysPermissions());
		if (!flag) {
			return renderError(ResultConstant.EXISTS_PERMISSION_NAME);
		}
		sysPermissionsParam.preInsert();
		sysPermissionsService.insertPermission(sysPermissionsParam.toSysPermissions());
		return renderSuccess(ResultConstant.INSERT_SUCCESS);
	}

	/**
	 * 进入查看页面
	 * 
	 * @param sysPermissionsParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("permissions_view")
	@GetMapping("/viewPage")
	public Object viewPage(SysPermissionsParam sysPermissionsParam, Model model, HttpServletRequest request) {
		sysPermissionsParam.setStatus(StringUtil.EMPTY);
		SysPermissions sysPermissions = sysPermissionsService.get(sysPermissionsParam.getId());
		model.addAttribute("info", sysPermissions);
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.TRUE);
		return "sys/permissions/edit";
	}

	/**
	 * 进入修改页面
	 * 
	 * @param sysPermissionsParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("permissions_update")
	@GetMapping("/editPage")
	public Object editPage(SysPermissionsParam sysPermissionsParam, Model model, HttpServletRequest request) {
		sysPermissionsParam.setStatus(StringUtil.EMPTY);
		SysPermissions sysPermissions = sysPermissionsService.get(sysPermissionsParam.getId());
		model.addAttribute("info", sysPermissions);
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.FALSE);
		return "sys/permissions/edit";
	}

	/**
	 * 修改
	 * 
	 * @param sysPermissionsParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("permissions_update")
	@PostMapping("/save")
	@ResponseBody
	public Object save(@Valid SysPermissionsParam sysPermissionsParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean flag = sysPermissionsService.checkPermissionName(sysPermissionsParam.toSysPermissions());
		if (!flag) {
			return renderError(ResultConstant.EXISTS_PERMISSION_NAME);
		}
		sysPermissionsParam.preUpdate();
		sysPermissionsService.updatePermissions(sysPermissionsParam.toSysPermissions());
		return renderSuccess(ResultConstant.UPDATE_SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param sysPermissionsParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("permissions_delete")
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(SysPermissionsParam sysPermissionsParam, BindingResult bindingResult) {
		boolean result = sysRoleService.checkUsePermission(sysPermissionsParam.getId());
		if (result) {
			return renderError(ResultConstant.USE_PERM);
		}
		sysPermissionsParam.preUpdate();
		sysPermissionsParam.setDelFlag(GlobalConstant.YES);
		sysPermissionsService.deletePermissions(sysPermissionsParam.toSysPermissions());
		return renderSuccess(ResultConstant.DELETE_SUCCESS);
	}

	/**
	 * 获取权限列表
	 * 
	 * @param sysPermissionsParam
	 * @return
	 */
	@PostMapping("/permList")
	@ResponseBody
	public Object selectPermList(SysPermissionsParam sysPermissionsParam) {
		return sysPermissionsService.listByEntity(sysPermissionsParam.toSysPermissions());
	}

}